package com.desire.controller;

import com.netflix.discovery.converters.Auto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.error.ErrorAttributeOptions;
import org.springframework.boot.web.servlet.error.ErrorAttributes;
import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.ServletWebRequest;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @Description:
 * @Author: littleDesire
 * @Date: 2022-06-10 10:18
 * @Version: 1.0
 */
@RestController
public class ZuulErrorController implements ErrorController {
    @Autowired
    ErrorAttributes errorAttributes;

    /**
     * 从2.3.0开始设置属性“server.error.path”
     * @return 未被使用
     */
    @Override
    public String getErrorPath() {
        return "/error";
    }

    @RequestMapping("/error")
    public Map<String, Object> error(HttpServletRequest request) {
        Map<String, Object> map = errorAttributes.getErrorAttributes(new ServletWebRequest(request), ErrorAttributeOptions.defaults());
        map.put("code", 0);
        return map;
    }
}
